package com.nmgsr.ptmsapp.config;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.nmgsr.beans.entity.PtmsUser;
import com.nmgsr.beans.entity.UserEntity;

public class PreferencesManager {
    private static final String PREF_NAME = "UserPreferences";
    private static final String KEY_TOKEN = "user_token";

    /**
     * 保存token
     *
     * @param context
     * @param token
     */
    public static void saveToken(Context context, String token) {
        SharedPreferences preferences = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString(KEY_TOKEN, token);
        editor.apply(); // 或者使用editor.commit()，但apply()是异步的，更高效
    }

    /**
     * 获取token
     *
     * @param context
     * @return
     */
    public static String getToken(Context context) {
        SharedPreferences preferences = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        return preferences.getString(KEY_TOKEN, "");
    }

    /**
     * 退出登录时清除token
     *
     * @param context
     */
    public static void clearToken(Context context) {
        SharedPreferences preferences = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.remove(KEY_TOKEN);
        editor.apply();
    }
    /**
     * 获取用户信息
     * @param applicationContext
     * @return
     */
    public static UserEntity getUserInfo(Context applicationContext) {
        SharedPreferences preferences = applicationContext.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        String userInfo = preferences.getString("userInfo", null);
        if (!TextUtils.isEmpty(userInfo)) {
            UserEntity user = new Gson().fromJson(userInfo, UserEntity.class);
            return user;
        } else {
            return null;
        }
    }
    /**
     * 保存用户信息
     * @param applicationContext
     * @param user
     */
    public static void setUserInfo(Context applicationContext, UserEntity user) {
        SharedPreferences preferences = applicationContext.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        String str = new Gson().toJson(user);
        editor.putString("userInfo", str);
        editor.apply();
    }
    /**
     * 保存用户名，密码，记住密码状态
     *
     * @param applicationContext
     * @param username
     */
    public static void saveUsername(Context applicationContext, String username, String password, boolean isRemember) {
        SharedPreferences preferences = applicationContext.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString("username", username);
        editor.putString("password", password);
        editor.putBoolean("isRemember", isRemember);
        editor.apply();
    }
    /**
     * 获取用户名，密码，记住密码状态
     *
     * @param applicationContext
     * @return
     */
    public static String[] getUsername(Context applicationContext) {
        SharedPreferences preferences = applicationContext.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        String username = preferences.getString("username", "");
        String password = preferences.getString("password", "");
        boolean isRemember = preferences.getBoolean("isRemember", false);
        if (isRemember) {
            return new String[]{username, password, "1"};
        } else {
            return new String[]{username, password, "0"};
        }
    }

    public static void saveUserInfo(Context applicationContext,PtmsUser user) {
        SharedPreferences preferences = applicationContext.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        String str = new Gson().toJson(user);
        editor.putString("userInfo", str);
        editor.apply();
    }

    public static PtmsUser getPtmsUserInfo(Context applicationContext) {
        SharedPreferences preferences = applicationContext.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        String userInfo = preferences.getString("userInfo", null);
        if (!TextUtils.isEmpty(userInfo)) {
            PtmsUser user = new Gson().fromJson(userInfo, PtmsUser.class);
            return user;
        } else {
            return null;
        }
    }
}
